package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.meta.business.client.api.ElevenDimensionsBusinessCardClient;
import com.meta.business.client.req.ObtainToken;
import com.meta.business.client.req.RegisterReq;
import com.meta.business.client.req.UserRoomsReq;
import com.meta.business.client.resp.GetTokenRes;
import com.meta.business.client.resp.WebUserRoomsRes;
import com.meta.component.base.api.CommonResult;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.pool.UserPool;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/space")
@Api(tags = "空间列表")
public class SpaceController {
    private final ElevenDimensionsBusinessCardClient elevenDimensionsBusinessCardClient;

    @Value("${dao.master.uuid}")
    private String daoMasterUUID;

    @PostMapping(value = "/userRooms", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户展馆列表", notes = "获取用户展馆列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<WebUserRoomsRes> userRooms(@RequestBody IdRequest idRequest) {
        UserInfo userInfo = UserPool.getUser();
        UserRoomsReq param = new UserRoomsReq();
        param.setOwner(daoMasterUUID);
        CommonResult<WebUserRoomsRes> webUserRoomsResCommonResult = elevenDimensionsBusinessCardClient.userRooms(param);
        log.info("空间:{}", JSON.toJSON(webUserRoomsResCommonResult));
        if (!webUserRoomsResCommonResult.isSuccessful()) {
            return Result.fail(webUserRoomsResCommonResult.getMessage());
        }
        ObtainToken obtainToken = new ObtainToken();
        obtainToken.setOwner(UserPool.getUser().getUserUuid());
        CommonResult<GetTokenRes> businessCardToken = elevenDimensionsBusinessCardClient.getBusinessCardToken(obtainToken);
        log.info("token:{}", JSON.toJSON(businessCardToken));
        String token="";
        if (businessCardToken.isSuccessful()) {
            token=businessCardToken.getData().getData().getToken();
            if (StrUtil.isBlank(token)) {
                // 用户注册,让后重新获取token
                RegisterReq register=new RegisterReq();
                register.setOwner(userInfo.getUserUuid());
                register.setNickName(userInfo.getNickName());
                register.setMobile(userInfo.getUserTel());
                register.setLogo(userInfo.getAvatarImg());
                elevenDimensionsBusinessCardClient.register(register);
                CommonResult<GetTokenRes> newBusinessCardToken = elevenDimensionsBusinessCardClient.getBusinessCardToken(obtainToken);
                token=newBusinessCardToken.getData().getData().getToken();
            }
        }
        WebUserRoomsRes data = webUserRoomsResCommonResult.getData();
        data.setUrl(StrUtil.format("{}&owner={}&token={}", data.getUrl(), UserPool.getUser().getUserUuid(), token));
        return Result.success(data);
    }
}

